package util

import (
	"fmt"
	"log"
	"math/rand"
	"mp4/entity"
	"time"
)

func Sleep(config entity.DownloadEntity, start time.Time) {
	val := Random(config)

	sleepAndPrint(val, start)
}

/*产生随机数*/
func Random(config entity.DownloadEntity) int32 {
	return RandomNum(config.MaxNum, config.MinNum)
}

/*产生随机数*/
func RandomNum(MaxNum int32, MinNum int32) int32 {
	return rand.Int31n(MaxNum-MinNum) + MinNum
}

/*打印暂停*/
func sleepAndPrint(value1 int32, start time.Time) {
	var i int32 = 0
	elapsed := time.Since(start)
	value := value1 - int32(elapsed.Seconds())
	log.Println("---->", value1, "-------wait value:", value, "-------elapsed times:", int32(elapsed.Seconds()))

	for i = 0; i < value; i++ {
		time.Sleep(time.Duration(1) * time.Second)

		if i%25 == 0 {
			fmt.Print("-")
		}
		if i%100 == 0 {
			fmt.Print(fmt.Sprintf("-%d", i))
		}
	}
}
